<template>
    <div class="app-container">
        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
            <el-form-item label="患者姓名" prop="patientName">
                <el-input
                        v-model="queryParams.patientName"
                        placeholder="请输入患者姓名"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item label="挂号单号" prop="regId">
                <el-input
                        v-model="queryParams.regId"
                        placeholder="请输入挂号单号"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="queryAll">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 数据表格开始 -->
        <el-table border :data="orderChargeTableList">
            <el-table-column label="订单号" align="center" width="210" prop="orderId"/>
            <el-table-column label="挂号单号" align="center" width="200" prop="regId"/>
            <el-table-column label="患者姓名" align="center" prop="patientName"/>
            <el-table-column label="总金额" align="center" prop="orderAmount"/>
            <el-table-column label="支付类型" align="center" prop="payType" :formatter="payTypeFormat"/>
            <el-table-column label="订单状态" prop="orderStatus" align="center" :formatter="orderStatusFormat"/>
            <el-table-column label="创建时间" align="center" prop="createTime"/>
            <el-table-column label="操作" width="300px" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-view" size="mini" @click="detailDemo(scope.row)">查看详情
                    </el-button>
                    <el-button type="text" :disabled="scope.row.orderStatus==='1'" icon="el-icon-bank-card" size="mini"
                               @click="handleXj(scope.row)">现金收费
                    </el-button>
                    <el-button type="text" :disabled="scope.row.orderStatus==='1'" icon="el-icon-bank-card" size="mini"
                               @click="handleEdit(scope.row)">支付宝收费
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页控件开始 -->
        <el-pagination
                v-show="total>0"
                :current-page="current"
                :page-sizes="[6, 10, 20, 30]"
                :page-size="size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->

        <!-- 查看详情的弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="1000px"
                center
                append-to-body
        >
            <el-table
                    v-loading="itemTableloading"
                    border
                    :data="orderChargeItemList"
            >
                <el-table-column label="详情ID" align="center" width="210" prop="itemId"/>
                <el-table-column label="处方ID" align="center" width="200" prop="coId"/>
                <el-table-column label="名称" align="center" prop="itemName"/>
                <el-table-column label="价格" align="center" prop="itemPrice"/>
                <el-table-column label="数量" align="center" prop="itemNum"/>
                <el-table-column label="小计" prop="itemAmount" align="center"/>
                <el-table-column label="类型" align="center" prop="itemType">
                    <template slot-scope="scope">
                        {{ scope.row.itemType==='0'?'药品':'检查' }}
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status" :formatter="statusFormatter"/>
            </el-table>
            <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </span>
        </el-dialog>
        <!-- 查看详情的弹出层结束 -->

        <!-- 微信支付的弹出层开始 -->
        <el-dialog
                title="请使用微信支付"
                :visible.sync="openPay"
                center
                :close-on-click-modal="false"
                :before-close="handleClose"
                append-to-body
        >
            <el-form label-position="left" label-width="120px" inline class="demo-table-expand">
                <el-card>
                    <el-form-item label="订单号:">
                        <span>{{ payObj.orderId }}</span>
                    </el-form-item>
                    <el-form-item label="总金额:">
                        <span>{{ payObj.price }}</span>
                    </el-form-item>
                </el-card>
            </el-form>
            <div style="text-align:center">
                <vue-qr :text="payObj.codeUrl" :size="200"/>
                <div>请使用微信扫码</div>
            </div>
        </el-dialog>
        <!-- 微信支付的弹出层结束 -->

    </div>
</template>

<script>
    import vueQr from "vue-qr"

    export default {
        name: "chargelist",
        components: {
            vueQr
        },
        data() {
            return {
                queryParams: {},
                //查询页面数据
                orderChargeTableList: [],
                //详情表单的数据
                orderChargeItemList: [],
                //查看详情标题
                title: '',
                open: false,
                //字典信息
                //支付类型
                payTypeOptions: [],
                //订单状态
                orderStatusOptions: [],
                //查看详情字典信息
                statusOptions: [],

                //支付对象
                payObj: {},
                //是否打开支付宝二维码支付层
                openPay: false,
                // 定时轮询对象
                intervalObj: undefined,

                //表单关闭
                itemTableloading: false,
                //分页参数
                current: 1,
                size: 6,
                total: 0

            }
        },
        created() {
            //查询全部
            this.initUser();

            //初始化支付字典
            this.initPayType();

            //初始化状态字典
            this.initOrderStatus();

            //初始化查看详情字典信息
            this.initStatus();
        },
        methods: {
            queryAll() {
                this.current = 1;
                this.initUser();
            },

            //表单重置
            resetForm() {
                this.queryParams = {};
                this.initUser();
            },

            //查询所有并分页
            initUser() {
                this.$axios.post("/charge/api/item/findAll/" + this.current + "/" + this.size, this.queryParams).then(result => {
                    this.orderChargeTableList = result.data.data.records;
                    this.total = result.data.data.total;
                })
            },

            //查看详情
            detailDemo(row) {
                this.title = '查询【' + row.patientName + '】支付订单的详情';
                this.open = true;
                this.itemTableloading = true;
                this.$axios.get('/charge/api/item/findAllUserId/' + row.orderId).then(result => {
                    this.orderChargeItemList = result.data.data;
                    this.itemTableloading = false;
                })

            },
            //支付宝支付
            handleEdit(row) {
                this.$confirm('是否确定支付宝支付', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.post('/charge/api/item/payForItem/' + row.orderId).then(res => {
                        this.payObj = res.data.data;
                        const tx = this
                        tx.openPay = true// 打开支付的弹出层
                        // 定时轮询
                        tx.intervalObj = setInterval(function () {
                            // 根据ID查询订单信息
                            tx.$axios.post('/charge/api/queryDataAll/' + tx.payObj.orderId).then(r => {
                                if (r.data.data === true) { // 说明订单状态为支付成功
                                    // 清空定时器
                                    clearInterval(tx.intervalObj)
                                    tx.$notify({
                                        title: '支付成功',
                                        message: '【' + tx.payObj.orderId + '】的订单编写支付成功',
                                        type: 'success'
                                    })
                                    tx.openPay = false
                                    tx.initUser();
                                }
                            }).catch(() => {
                                // 清空定时器
                                clearInterval(tx.intervalObj)
                            })
                        }, 2000)
                    }).catch(() => {
                        this.$message('操作失败')
                    })
                })
            },

            handleXj(row) {
                this.$confirm('是否确定现金支付', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.get('/charge/api/item/CashCharges/' + row.orderId).then(result => {
                        this.msgSuccess('现金支付成功')
                        this.initUser();
                    }).catch(() => {
                        this.$message('现金支付失败')
                    })
                }).catch(() => {
                    this.$message('现金支付取消')
                })

            },

            //支付字典表的赋值
            initPayType() {
                this.$axios.get("/charge/api/item/findAllByType/his_pay_type_status").then(result => {
                    this.payTypeOptions = result.data.data;
                })
            },
            //订单状态字典表的赋值
            initOrderStatus() {
                this.$axios.get('/charge/api/item/findAllByType/his_order_charge_status').then(result => {
                    this.orderStatusOptions = result.data.data;
                })
            },
            initStatus() {
                this.$axios.get('/charge/api/item/findAllByType/his_order_details_status').then(result => {
                    this.statusOptions = result.data.data;
                })
            },
            //赋值给payType并渲染到前端页面
            payTypeFormat(row) {
                let p = row.payType;
                return this.formatDict(this.payTypeOptions, p);
            },

            orderStatusFormat(row) {
                let o = row.orderStatus;
                return this.formatDict(this.orderStatusOptions, o);
            },
            statusFormatter(row) {
                let s = row.status;
                return this.formatDict(this.statusOptions, s);
            },

            cancel() {
                this.open = false;
            },

            //分页显示状态
            handleSizeChange(v) {
                this.size = v;
                this.initUser();
            },
            handleCurrentChange(row) {
                this.current = row;
                this.initUser();
            },

            handleClose() {
                this.$confirm('您确定放弃支付吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$message('您已放弃支付，可以回到收费查询列表里面再次支付')
                    this.initUser();
                    this.openPay = false
                    // 关闭轮询
                    clearInterval(this.intervalObj)
                }).catch(() => {
                    this.msgSuccess('欢迎继续支付')
                })
            }

        }
    }
</script>

<style scoped>

</style>
